package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.ReplacePartNumber;
import com.scalar.forecast.projectSetup.MapPartNumbers;
import com.scalar.forecast.projectSetup.forms.MapPartNumbersForm;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * Processes the request to Map the Project Setup Part Numbers
 *
 * Created by Veeru.
 * Date: Oct 10, 2005
 * Time: 4:47:45 PM
 */


public class MapPartNumbersCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "mapPartNumbers" );
        MapPartNumbersForm mapPartNumbersForm = ( MapPartNumbersForm )form;
        MapPartNumbers mapPartNumbers = mapPartNumbersForm.getMapPartNumbers();
        int projectId;
        if ( request.getParameter("projectID") != null ) {
            try
            {
                projectId = Integer.parseInt( request.getParameter("projectID").trim() );
            }
            catch( Exception e )
            {
                projectId = -1;
            }
            try
            {
                ArrayList chipsList = new ArrayList();
                mapPartNumbersForm.setProjectId( projectId );
                ArrayList selectedChipsList = ProjectSetupDAO.loadSelectedProducts( null, projectId, "CHIP" );
                for (int i = 0; i < selectedChipsList.size(); i++) {
                    LabelValueBean bean = (LabelValueBean) selectedChipsList.get(i);
                    chipsList.add( new ReplacePartNumber( false, Integer.parseInt( bean.getValue()) , -1, bean.getLabel() ) );
                }
                ArrayList boardsList = new ArrayList();
                ArrayList selectedBoardsList = ProjectSetupDAO.loadSelectedProducts( null, projectId, "BOARD" );
                for (int i = 0; i < selectedBoardsList.size(); i++) {
                    LabelValueBean bean = (LabelValueBean) selectedBoardsList.get(i);
                    boardsList.add( new ReplacePartNumber( false, Integer.parseInt( bean.getValue()) , -1, bean.getLabel() ) );
                }
                ArrayList kitsList = new ArrayList();
                ArrayList selectedKitsList = ProjectSetupDAO.loadSelectedProducts( null, projectId, "KIT" );
                for (int i = 0; i < selectedKitsList.size(); i++) {
                    LabelValueBean bean = (LabelValueBean) selectedKitsList.get(i);
                    kitsList.add( new ReplacePartNumber( false, Integer.parseInt( bean.getValue()) , -1, bean.getLabel() ) );
                }
                mapPartNumbers.setChipsList( chipsList );
                mapPartNumbers.setBoardsList( ( boardsList ) );
                mapPartNumbers.setKitsList( ( kitsList ) );

                request.setAttribute( "availableChipsList" , ProjectSetupDAO.loadAllProducts( null, "CHIP") );
                request.setAttribute( "availableBoardsList" , ProjectSetupDAO.loadAllProducts( null, "BOARD") );
                request.setAttribute( "availableKitsList" , ProjectSetupDAO.loadAllProducts( null, "KIT") );
                if ( chipsList.size() > 0 )
                {
                    request.setAttribute("showChips", "TRUE");
                }
                if ( boardsList.size() > 0 )
                {
                    request.setAttribute("showBoards", "TRUE");
                }
                if ( kitsList.size() > 0 )
                {
                    request.setAttribute("showKits", "TRUE");
                }
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
        return actionForward;
    }
}


